package com.ruoyi.web.controller.gcgl;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.config.ServerConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ConvertUpMoneyUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.DecimalCalculateUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.gcgl.domain.GcBszbzfd;
import com.ruoyi.gcgl.domain.GcBszbzfditem;
import com.ruoyi.gcgl.domain.GcProject;
import com.ruoyi.gcgl.domain.GcSupply;
import com.ruoyi.gcgl.domain.SjProcessConfig;
import com.ruoyi.gcgl.domain.SjProjectPeople;
import com.ruoyi.gcgl.service.IGcBszbzfdService;
import com.ruoyi.gcgl.service.IGcBszbzfditemService;
import com.ruoyi.gcgl.service.IGcProjectService;
import com.ruoyi.gcgl.service.IGcSendTipService;
import com.ruoyi.gcgl.service.IGcSupplyService;
import com.ruoyi.gcgl.service.ISjProcessConfigService;
import com.ruoyi.gcgl.service.ISjProjectPeopleService;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysRoleService;

/**
 * 标书中标支付单Controller
 * 
 * @author ruoyi
 * @date 2024-07-22
 */
@Controller
@RequestMapping("/gcgl/bszbzfd")
public class GcBszbzfdController extends BaseController
{
    private String prefix = "gcgl/bszbzfd";

    @Autowired
    private IGcBszbzfdService gcBszbzfdService;
    @Autowired
    private IGcBszbzfditemService gcBszbzfditemService;
    @Autowired
    private IGcProjectService gcProjectService;
    @Autowired
    private IGcSendTipService gcSendTipService;
    @Autowired
    private IGcSupplyService gcSupplyService;
    @Autowired
    private ServerConfig serverConfig;
    @Resource
    private RuntimeService runtimeService;
    @Resource
    private TaskService taskService;
    @Autowired
    private ISjProcessConfigService sjProcessConfigService;
    @Autowired
    private ISysDeptService deptService;
    @Autowired
    private ISysRoleService roleService;
    
    final String processCode = "sjbaoxiao";
    @Autowired
    private ISjProjectPeopleService sjProjectPeopleService;
    
    private static final String FILE_DELIMETER = ",";

    @RequiresPermissions("gcgl:bszbzfd:view")
    @GetMapping()
    public String bszbzfd(ModelMap mmap)
    {
        return prefix + "/bszbzfd";
    }

    /**
     * 查询其它付款申请单列表
     */
    @RequiresPermissions("gcgl:bszbzfd:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(GcBszbzfd gcBszbzfd)
    {
        startPage();
        gcBszbzfd.setDeleteFlag(1);
        //角色数据权限 ==根据用户定义的数据权限查看项目数据
        List<SysRole> roles = getSysUser().getRoles();
        List<Long> roleIds = new ArrayList<Long>();
        for(SysRole obj:roles){
        	roleIds.add(obj.getRoleId());
        }
        
        int count = roleService.selectCountRoleDeptByRoleId(roleIds);
        if(count != 0){
        	//看部门
        	gcBszbzfd.setRoleIds(roleIds);
        }else{
        	//看个人
        	if(getSysUser().isShowRoleIds(roleIds)){
        		gcBszbzfd.setOwnerId(getUserId());
            }
        }
        //----
        
        List<GcBszbzfd> list = gcBszbzfdService.selectGcBszbzfdList(gcBszbzfd);
        return getDataTable(list);
    }

    /**
     * 导出其它付款申请单列表
     */
    @RequiresPermissions("gcgl:bszbzfd:export")
    @Log(title = "标书中标支付费", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(GcBszbzfd gcBszbzfd)
    {
    	gcBszbzfd.setDeleteFlag(1);
        if(getSysUser().isShow()){
        	gcBszbzfd.setCreateUser(getLoginName());
        }
        List<GcBszbzfd> list = gcBszbzfdService.selectGcBszbzfdList(gcBszbzfd);
        ExcelUtil<GcBszbzfd> util = new ExcelUtil<GcBszbzfd>(GcBszbzfd.class);
        return util.exportExcel(list, "标书中标支付费数据");
    }

    /**
     * 新增标书中标支付单
     */
    @GetMapping("/add/{id}")
    public String add(ModelMap mmap,@PathVariable("id") Long id){
    	mmap.put("fksqr", getLoginName());
    	
    	GcSupply gcSupply = new GcSupply();
    	gcSupply.setDeleteFlag(1);
//    	gcSupply.setType(6);
    	List<GcSupply> gcSupplylis = gcSupplyService.selectGcSupplyList(gcSupply);
    	mmap.put("clis", gcSupplylis);
    	
    	GcProject po = gcProjectService.selectGcProjectById(id);
    	mmap.put("gcProject", po);
    	
    	mmap.put("owner", getLoginName());
    	mmap.put("ownerId", getUserId());
    	mmap.put("deptName", getSysUser().getDept().getDeptName());
    	mmap.put("deptId", getSysUser().getDept().getDeptId());
    	
    	//下一级审核人
        List<SjProcessConfig> configlis = returnUser(getSysUser().getDept().getDeptId());
        mmap.put("approveid", configlis.get(0).getLevel1id());
        mmap.put("approvename", configlis.get(0).getLevel1name());
        
      //所属
        SysDept dept = new SysDept();
        dept.setDelFlag("1");
        dept.setParentId(100l);
        List<SysDept> deptlis = deptService.selectDeptList4pId(dept);
        mmap.put("deptlis", deptlis);
    	
        return prefix + "/add";
    }

    /**
     * 新增保存标书中标支付单
     */
    @RequiresPermissions("gcgl:bszbzfd:add")
    @Log(title = "标书中标支付费", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public synchronized AjaxResult addSave(GcBszbzfd gcBszbzfd){
    	try {
    		SimpleDateFormat sdy = new SimpleDateFormat("yyyy");
    		SimpleDateFormat sdm = new SimpleDateFormat("MM");
    		int year = Integer.parseInt(sdy.format(new Date()));
    		int month = Integer.parseInt(sdm.format(new Date()));
    		GcBszbzfd newObj = new GcBszbzfd();
    		newObj.setDeleteFlag(1);
    		newObj.setCreateAt(DateUtils.getFisrtDayOfMonth(year, month));
    		newObj.setUpdateAt(DateUtils.getLastDayOfMonth(year, month));
    		List<GcBszbzfd> lisCount = gcBszbzfdService.selectGcBszbzfdList(newObj);
    		
        	GcSupply gcSupply = new GcSupply();
        	gcSupply.setDeleteFlag(1);
        	gcSupply.setName(gcBszbzfd.getSkdw());
        	List<GcSupply> gcSupplylis = gcSupplyService.selectGcSupplyList(gcSupply);
        	
    		if(gcSupplylis!=null && gcSupplylis.size()>0){
    			gcBszbzfd.setSkdw(gcSupplylis.get(0).getName());
    			gcBszbzfd.setSkzh(gcSupplylis.get(0).getSkzh());
    			gcBszbzfd.setNsrsbh(gcSupplylis.get(0).getNssbh());
    			gcBszbzfd.setKhyh(gcSupplylis.get(0).getKhyh());
    		}
        	
    		String no = String.format("%02d", lisCount.size()+1);
    		String l_month = String.format("%02d", month);
    		gcBszbzfd.setOrderNo(year+"-"+l_month+"-"+no);
    		gcBszbzfd.setDeleteFlag(1);
    		gcBszbzfd.setCreateAt(new Date());
    		gcBszbzfd.setUpdateAt(new Date());
    		gcBszbzfd.setCreateUser(getLoginName());
    		gcBszbzfd.setUpdateUser(getLoginName());
    		gcBszbzfd.setSqr(getLoginName());
    		gcBszbzfd.setSqrAt(new Date());
    		gcBszbzfd.setStatus(1);
    		
//    		GcProject gcProject = gcProjectService.selectGcProjectById(Long.parseLong(gcbszbzfd.getGcProjectId().toString()));
//    		gcbszbzfd.setGcProjectName(gcProject.getName());
    		
    		gcBszbzfdService.insertGcBszbzfd(gcBszbzfd);
    		
    		Double hejexx = 0.00;
        	for(GcBszbzfditem obj:gcBszbzfd.getGcBszbzfditem()){
        		hejexx=DecimalCalculateUtil.add(hejexx, 
        				Double.parseDouble(obj.getJine().toString()));
        		obj.setDeleteFlag(1);
        		obj.setCreateAt(new Date());
        		obj.setUpdateAt(new Date());
        		obj.setCreateUser(getLoginName());
        		obj.setUpdateUser(getLoginName());
        		obj.setBszbzfdId(gcBszbzfd.getId());
        		gcBszbzfditemService.insertGcBszbzfditem(obj);
        	}

			DecimalFormat mf = new DecimalFormat();
			mf.applyPattern("#0.00");
        	gcBszbzfd.setHejexx(mf.format(hejexx));
        	gcBszbzfd.setHeje(ConvertUpMoneyUtil.toChinese(hejexx.toString()));
        	gcBszbzfdService.updateGcBszbzfd(gcBszbzfd);
        	
        	//发送消息通知
        	gcSendTipService.sendTipXm("qtfk", "标书中标支付费", gcBszbzfd.getOrderNo(), 
        			gcBszbzfd.getCreateUser(), DateUtils.dateTime(gcBszbzfd.getCreateAt()), 
        			gcBszbzfd.getStatus().toString(), null, gcBszbzfd.getId().toString());
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return toAjax(0);
		}
    	
        return toAjax(1);
    }

    /**
     * 修改标书中标支付单
     */
    @RequiresPermissions("gcgl:bszbzfd:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        GcBszbzfd gcbszbzfd = gcBszbzfdService.selectGcBszbzfdById(id);
        GcBszbzfditem gcbszbzfditem = new GcBszbzfditem();
        gcbszbzfditem.setBszbzfdId(id);
        List<GcBszbzfditem> items = gcBszbzfditemService.selectGcBszbzfditemList(gcbszbzfditem);
        gcbszbzfd.setGcBszbzfditem(items);
        
        if(gcbszbzfd.getZilwj()!=null && !gcbszbzfd.getZilwj().equals("")){
        	String[] zlis = gcbszbzfd.getZilwj().split(",");
        	String file = "";
        	for(int i=0;i<zlis.length;i++){
        		file += zlis[i]+",";
        	}
        	gcbszbzfd.setZilwj(file);
        }
        
        mmap.put("gcBszbzfd", gcbszbzfd);
        
        GcSupply gcSupply = new GcSupply();
    	gcSupply.setDeleteFlag(1);
//    	gcSupply.setType(6);
    	List<GcSupply> gcSupplylis = gcSupplyService.selectGcSupplyList(gcSupply);
    	mmap.put("clis", gcSupplylis);
    	
    	//下一级审核人
        List<SjProcessConfig> configlis = returnUser(getSysUser().getDept().getDeptId());
        mmap.put("approveid", configlis.get(0).getLevel1id());
        mmap.put("approvename", configlis.get(0).getLevel1name());
        
      //所属
        SysDept dept = new SysDept();
        dept.setDelFlag("1");
        dept.setParentId(100l);
        List<SysDept> deptlis = deptService.selectDeptList4pId(dept);
        mmap.put("deptlis", deptlis);
    	
        return prefix + "/edit";
    }

    /**
     * 修改保存标书中标支付单
     */
    @RequiresPermissions("gcgl:bszbzfd:edit")
    @Log(title = "标书中标支付单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(GcBszbzfd gcbszbzfd)
    {
    	try {
    		gcBszbzfditemService.deleteGcBszbzfditemById(gcbszbzfd.getId());
    		
    		Double hejexx = 0.00;
        	//item
        	for(GcBszbzfditem item : gcbszbzfd.getGcBszbzfditem()){
        		hejexx=DecimalCalculateUtil.add(hejexx, 
        				Double.parseDouble(item.getJine().toString()));
        		item.setDeleteFlag(1);
        		item.setUpdateAt(new Date());
        		item.setCreateUser(getLoginName());
        		item.setUpdateUser(getLoginName());
        		item.setBszbzfdId(gcbszbzfd.getId());
        		gcBszbzfditemService.insertGcBszbzfditem(item);
        	}
        	
        	GcSupply gcSupply = new GcSupply();
        	gcSupply.setDeleteFlag(1);
        	gcSupply.setName(gcbszbzfd.getSkdw());
        	List<GcSupply> gcSupplylis = gcSupplyService.selectGcSupplyList(gcSupply);
        	
    		if(gcSupplylis!=null && gcSupplylis.size()>0){
    			gcbszbzfd.setSkdw(gcSupplylis.get(0).getName());
            	gcbszbzfd.setSkzh(gcSupplylis.get(0).getSkzh());
            	gcbszbzfd.setNsrsbh(gcSupplylis.get(0).getNssbh());
            	gcbszbzfd.setKhyh(gcSupplylis.get(0).getKhyh());
    		}
    		
    		DecimalFormat mf = new DecimalFormat();
			mf.applyPattern("#0.00");
        	gcbszbzfd.setHejexx(mf.format(hejexx));
        	gcbszbzfd.setHeje(ConvertUpMoneyUtil.toChinese(hejexx.toString()));
        	gcbszbzfd.setUpdateAt(new Date());
        	gcbszbzfd.setUpdateUser(getLoginName());
        	
//        	GcProject obj = gcProjectService.selectGcProjectById(Long.parseLong(gcbszbzfd.getGcProjectId().toString()));
//    		gcbszbzfd.setGcProjectName(obj.getName());
        	
        	gcBszbzfdService.updateGcBszbzfd(gcbszbzfd);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return toAjax(0);
		}
        return toAjax(1);
    }

    /**
     * 删除标书中标支付单
     */
    @RequiresPermissions("gcgl:bszbzfd:remove")
    @Log(title = "标书中标支付单", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(gcBszbzfdService.deleteGcBszbzfdByIds(ids));
    }
    
    /////财务审批
    @RequiresPermissions("gcgl:bszbzfd:cwpsview")
    @GetMapping("/cwps")
    public String cwps()
    {
        return prefix + "/bszbzfdcwps";
    }
    
    @PostMapping("/cwpslist")
    @ResponseBody
    public TableDataInfo cwpslist(GcBszbzfd gcbszbzfd)
    {
        startPage();
        gcbszbzfd.setDeleteFlag(1);
//        gcbszbzfd.setStatus(1);
        List<GcBszbzfd> list = gcBszbzfdService.selectGcBszbzfdList(gcbszbzfd);
        return getDataTable(list);
    }
    
    @Log(title = "通过", businessType = BusinessType.UPDATE)
    @PostMapping("/cwyes")
    @ResponseBody
    public AjaxResult cwyes(String ids, ModelMap mmap){
    	String[] id = Convert.toStrArray(ids);
    	for(int i=0;i<id.length;i++){
    		GcBszbzfd obj = new GcBszbzfd();
    		obj.setId(Long.parseLong(id[i]));
    		obj.setStatus(1);
    		obj.setCwsh(getLoginName());
    		obj.setCwshAt(new Date());
    		gcBszbzfdService.updateGcBszbzfd(obj);
    		
    		GcBszbzfd gcbszbzfd = gcBszbzfdService.selectGcBszbzfdById(Long.parseLong(id[i]));
    		//发送消息通知
        	gcSendTipService.sendTipXm("qtfk", "标书中标支付单", gcbszbzfd.getOrderNo(), 
        			gcbszbzfd.getCreateUser(), DateUtils.dateTime(gcbszbzfd.getCreateAt()), 
        			gcbszbzfd.getStatus().toString(), null, gcbszbzfd.getId().toString());
    	}
    	
    	return toAjax(1);
    }
    
    @Log(title = "不通过", businessType = BusinessType.UPDATE)
    @PostMapping("/cwno")
    @ResponseBody
    public AjaxResult cwno(String ids, ModelMap mmap){
    	String[] id = Convert.toStrArray(ids);
    	for(int i=0;i<id.length;i++){
    		GcBszbzfd obj = new GcBszbzfd();
    		obj.setId(Long.parseLong(id[i]));
    		obj.setStatus(2);
    		obj.setCwsh(getLoginName());
    		obj.setCwshAt(new Date());
    		gcBszbzfdService.updateGcBszbzfd(obj);
    		
    		GcBszbzfd gcbszbzfd = gcBszbzfdService.selectGcBszbzfdById(Long.parseLong(id[i]));
    		//发送消息通知
        	gcSendTipService.sendTipXm("qtfk", "标书中标支付单", gcbszbzfd.getOrderNo(), 
        			gcbszbzfd.getCreateUser(), DateUtils.dateTime(gcbszbzfd.getCreateAt()), 
        			gcbszbzfd.getStatus().toString(), null, gcbszbzfd.getId().toString());
    	}
    	return toAjax(1);
    }
    
    /////总经理审批
    @RequiresPermissions("gcgl:bszbzfd:zjlpsview")
    @GetMapping("/zjlps")
    public String zjlps()
    {
        return prefix + "/bszbzfdzjlps";
    }
    
    @PostMapping("/zjlpslist")
    @ResponseBody
    public TableDataInfo zjlpslist(GcBszbzfd gcbszbzfd)
    {
        startPage();
        gcbszbzfd.setDeleteFlag(1);
        gcbszbzfd.setStatus(1);
        List<GcBszbzfd> list = gcBszbzfdService.selectGcBszbzfdList(gcbszbzfd);
        return getDataTable(list);
    }
    
    @Log(title = "通过", businessType = BusinessType.UPDATE)
    @PostMapping("/zjlyes")
    @ResponseBody
    public AjaxResult zjlyes(String ids, ModelMap mmap){
    	String[] id = Convert.toStrArray(ids);
    	for(int i=0;i<id.length;i++){
    		GcBszbzfd obj = new GcBszbzfd();
    		obj.setId(Long.parseLong(id[i]));
    		obj.setStatus(3);
    		obj.setLdsp(getLoginName());
    		obj.setLdspAt(new Date());
    		gcBszbzfdService.updateGcBszbzfd(obj);
    		
    		GcBszbzfd gcbszbzfd = gcBszbzfdService.selectGcBszbzfdById(Long.parseLong(id[i]));
    		//发送消息通知
        	gcSendTipService.sendTipXm("qtfk", "标书中标支付单", gcbszbzfd.getOrderNo(), 
        			gcbszbzfd.getCreateUser(), DateUtils.dateTime(gcbszbzfd.getCreateAt()), 
        			gcbszbzfd.getStatus().toString(), null, gcbszbzfd.getId().toString());
    	}
    	
    	return toAjax(1);
    }
    
    @Log(title = "不通过", businessType = BusinessType.UPDATE)
    @PostMapping("/zjlno")
    @ResponseBody
    public AjaxResult zjlno(String ids, ModelMap mmap){
    	String[] id = Convert.toStrArray(ids);
    	for(int i=0;i<id.length;i++){
    		GcBszbzfd obj = new GcBszbzfd();
    		obj.setId(Long.parseLong(id[i]));
    		obj.setStatus(4);
    		obj.setLdsp(getLoginName());
    		obj.setLdspAt(new Date());
    		gcBszbzfdService.updateGcBszbzfd(obj);
    		
    		GcBszbzfd gcbszbzfd = gcBszbzfdService.selectGcBszbzfdById(Long.parseLong(id[i]));
    		//发送消息通知
        	gcSendTipService.sendTipXm("qtfk", "标书中标支付单", gcbszbzfd.getOrderNo(), 
        			gcbszbzfd.getCreateUser(), DateUtils.dateTime(gcbszbzfd.getCreateAt()), 
        			gcbszbzfd.getStatus().toString(), null, gcbszbzfd.getId().toString());
    	}
    	return toAjax(1);
    }
    
    @GetMapping("/bszbzfdprint/{id}")
    public String bszbzfdprint(@PathVariable("id") Long id, ModelMap mmap){
    	GcBszbzfd obj = gcBszbzfdService.selectGcBszbzfdById(id);
    	
    	if(obj.getAuthority()==null || obj.getAuthority().toString().equals("")){
    		obj.setAuthorityName("");
    	}else{
    		SysDept dept = deptService.selectDeptById(Long.parseLong(obj.getAuthority().toString()));
        	obj.setAuthorityName(dept.getDeptName());
    	}
        
    	GcBszbzfditem items = new GcBszbzfditem();
        items.setBszbzfdId(id);
        List<GcBszbzfditem> itemlis= gcBszbzfditemService.selectGcBszbzfditemList(items);
        if(itemlis!=null){
        	obj.setGcBszbzfditem(itemlis);
        }
        
        mmap.put("obj", obj);
        return prefix + "/bszbzfdprint";
    }
    
    @GetMapping("/bszbzfdcwsp/{id}")
    public String bszbzfdcwsp(@PathVariable("id") Long id, ModelMap mmap){
    	GcBszbzfd obj = gcBszbzfdService.selectGcBszbzfdById(id);
        
    	GcBszbzfditem items = new GcBszbzfditem();
        items.setBszbzfdId(id);
        List<GcBszbzfditem> itemlis= gcBszbzfditemService.selectGcBszbzfditemList(items);
        if(itemlis!=null){
        	obj.setGcBszbzfditem(itemlis);
        }
        
        mmap.put("obj", obj);
        return prefix + "/bszbzfdcwsp";
    }
    
    @GetMapping("/bszbzfdzjlsp/{id}")
    public String bszbzfdzjlsp(@PathVariable("id") Long id, ModelMap mmap){
    	GcBszbzfd obj = gcBszbzfdService.selectGcBszbzfdById(id);
        
    	GcBszbzfditem items = new GcBszbzfditem();
        items.setBszbzfdId(id);
        List<GcBszbzfditem> itemlis= gcBszbzfditemService.selectGcBszbzfditemList(items);
        if(itemlis!=null){
        	obj.setGcBszbzfditem(itemlis);
        }
        
        mmap.put("obj", obj);
        return prefix + "/bszbzfdzjlsp";
    }
    
    /**
     * 通用上传请求（多个）
     */
    @PostMapping("/uploads")
    @ResponseBody
    public AjaxResult uploadFiles(List<MultipartFile> files) throws Exception{
        try{
            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            List<String> urls = new ArrayList<String>();
            List<String> fileNames = new ArrayList<String>();
            List<String> newFileNames = new ArrayList<String>();
            List<String> originalFilenames = new ArrayList<String>();
            for (MultipartFile file : files)
            {
                // 上传并返回新文件名称
                String fileName = FileUploadUtils.upload(filePath, file);
                String url = serverConfig.getUrl() + fileName;
                urls.add(url);
                fileNames.add(fileName);
                newFileNames.add(FileUtils.getName(fileName));
                originalFilenames.add(file.getOriginalFilename());
            }
            AjaxResult ajax = AjaxResult.success();
            ajax.put("urls", StringUtils.join(urls, FILE_DELIMETER));
            ajax.put("fileNames", StringUtils.join(fileNames, FILE_DELIMETER));
            ajax.put("newFileNames", StringUtils.join(newFileNames, FILE_DELIMETER));
            ajax.put("originalFilenames", StringUtils.join(originalFilenames, FILE_DELIMETER));
            return ajax;
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
    }
    
    @GetMapping("/p4bszbzfd/{id}")
    public String p4bszbzfd(ModelMap mmap,@PathVariable("id") Long id){
    	GcProject po = gcProjectService.selectGcProjectById(id);
    	mmap.put("gcProject", po);
    	
    	//判断是成员，还是管理员
    	List<SysRole> roles = getSysUser().getRoles();
        List<Long> roleIds = new ArrayList<Long>();
        for(SysRole obj:roles){
        	roleIds.add(obj.getRoleId());
        }
        
    	//看个人
        int flag = 0;//成员
    	if(getSysUser().isShowRoleIds(roleIds)){
    		SjProjectPeople sjProjectPeople = new SjProjectPeople();
            sjProjectPeople.setDeleteFlag(1);
            sjProjectPeople.setGcProjectid(id);
            sjProjectPeople.setCode(getUserId().toString());
            List<SjProjectPeople> peoplelis = sjProjectPeopleService.selectSjProjectPeopleList(sjProjectPeople);
            if(peoplelis!=null && peoplelis.size()>0){
            	for(SjProjectPeople po1:peoplelis){
                	if(po1.getpId()==0l){
                		flag = 1;//领导
                		break;
                	}
                }
            }
        }else{
        	flag = 1;//领导
        }
        mmap.put("flag", flag);
        return "sj/project/projectDetailBszbzfd";
    }

    /**
     * 查询其它付款申请单列表
     */
    @PostMapping("/p4list")
    @ResponseBody
    public TableDataInfo p4list(GcBszbzfd gcBszbzfd){
        startPage();
        gcBszbzfd.setDeleteFlag(1);
      //角色数据权限 ==根据用户定义的数据权限查看项目数据
        List<SysRole> roles = getSysUser().getRoles();
        List<Long> roleIds = new ArrayList<Long>();
        for(SysRole obj:roles){
        	roleIds.add(obj.getRoleId());
        }
        
        int count = roleService.selectCountRoleDeptByRoleId(roleIds);
        if(count != 0){
        	//看部门
        	gcBszbzfd.setRoleIds(roleIds);
        }else{
        	//看个人
        	if(getSysUser().isShowRoleIds(roleIds)){
        		gcBszbzfd.setOwnerId(getUserId());
            }
        }
        //----
        List<GcBszbzfd> list = gcBszbzfdService.selectGcBszbzfdList(gcBszbzfd);
        return getDataTable(list);
    }
    
  //---------------------审批方法
    //申请人部门PID
    private List<SjProcessConfig> returnUser(Long deptId){
    	SysDept dept = deptService.selectDeptById(deptId);
    	SjProcessConfig sjProcessConfig = new SjProcessConfig();
        sjProcessConfig.setDeleteFlag(1);
        sjProcessConfig.setCode(processCode);
        sjProcessConfig.setDeptId(dept.getParentId());
        List<SjProcessConfig> configlis = sjProcessConfigService.selectSjProcessConfigList(sjProcessConfig);
		
        if(configlis==null || configlis.size()==0){
        	SjProcessConfig sjProcessConfig1 = new SjProcessConfig();
        	sjProcessConfig1.setDeleteFlag(1);
        	sjProcessConfig1.setCode("morenliucheng");
            List<SjProcessConfig> configlis1 = sjProcessConfigService.selectSjProcessConfigList(sjProcessConfig1);
            return configlis1;
        }
        return configlis;
    }
    
    /**
     * 修改保存项目立项
     */
    @Log(title = "中标服务费", businessType = BusinessType.UPDATE)
    @PostMapping("/edit4check")
    @ResponseBody
    public AjaxResult edit4check(GcBszbzfd gcBszbzfd){
    	try {
        	gcBszbzfd.setUpdateAt(new Date());
        	gcBszbzfd.setUpdateUser(getLoginName());
        	gcBszbzfdService.updateGcBszbzfd4Check(gcBszbzfd);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return toAjax(0);
		}
        return toAjax(1);
    }
    
    /**
     * level1
     * @return
     */
    @GetMapping("/level1")
    public String level1(String taskid, ModelMap mmap){
        Task t = taskService.createTaskQuery().taskId(taskid).singleResult();
        String processId = t.getProcessInstanceId();
        ProcessInstance p = runtimeService.createProcessInstanceQuery().processInstanceId(processId).singleResult();
        if (p != null) {
        	GcBszbzfd gcBszbzfd = gcBszbzfdService.selectGcBszbzfdById(Long.parseLong(p.getBusinessKey().toString()));
        	GcBszbzfditem gcBszbzfditem = new GcBszbzfditem();
        	gcBszbzfditem.setBszbzfdId(gcBszbzfd.getId());
            List<GcBszbzfditem> items = gcBszbzfditemService.selectGcBszbzfditemList(gcBszbzfditem);
            gcBszbzfd.setGcBszbzfditem(items);
            
            if(gcBszbzfd.getZilwj()!=null && !gcBszbzfd.getZilwj().equals("")){
            	String[] zlis = gcBszbzfd.getZilwj().split(",");
            	String file = "";
            	for(int i=0;i<zlis.length;i++){
            		file += zlis[i]+",";
            	}
            	gcBszbzfd.setZilwj(file);
            }
            mmap.put("obj", gcBszbzfd);
        	
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            mmap.put("taskid", taskid);
            
            //下一级审核人
            List<SjProcessConfig> configlis = returnUser(Long.valueOf(gcBszbzfd.getDeptId().toString()));
            mmap.put("approveid", configlis.get(0).getLevel2id());
            mmap.put("approvename", configlis.get(0).getLevel2name());
            
          //所属
            SysDept dept = new SysDept();
            dept.setDelFlag("1");
            dept.setParentId(100l);
            List<SysDept> deptlis = deptService.selectDeptList4pId(dept);
            mmap.put("deptlis", deptlis);
        }
        
        return prefix + "/level1check";
    }
    
    /**
     * level1
     * @return
     */
    @GetMapping("/level2")
    public String level2(String taskid, ModelMap mmap){
        Task t = taskService.createTaskQuery().taskId(taskid).singleResult();
        String processId = t.getProcessInstanceId();
        ProcessInstance p = runtimeService.createProcessInstanceQuery().processInstanceId(processId).singleResult();
        if (p != null) {
        	GcBszbzfd gcBszbzfd = gcBszbzfdService.selectGcBszbzfdById(Long.parseLong(p.getBusinessKey().toString()));
        	GcBszbzfditem gcBszbzfditem = new GcBszbzfditem();
        	gcBszbzfditem.setBszbzfdId(gcBszbzfd.getId());
            List<GcBszbzfditem> items = gcBszbzfditemService.selectGcBszbzfditemList(gcBszbzfditem);
            gcBszbzfd.setGcBszbzfditem(items);
            
            if(gcBszbzfd.getZilwj()!=null && !gcBszbzfd.getZilwj().equals("")){
            	String[] zlis = gcBszbzfd.getZilwj().split(",");
            	String file = "";
            	for(int i=0;i<zlis.length;i++){
            		file += zlis[i]+",";
            	}
            	gcBszbzfd.setZilwj(file);
            }
            mmap.put("obj", gcBszbzfd);
        	
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            mmap.put("taskid", taskid);
            
            //下一级审核人
            mmap.put("approveid", gcBszbzfd.getHjId());
            mmap.put("approvename", gcBszbzfd.getHj());
            
          //所属
            SysDept dept = new SysDept();
            dept.setDelFlag("1");
            dept.setParentId(100l);
            List<SysDept> deptlis = deptService.selectDeptList4pId(dept);
            mmap.put("deptlis", deptlis);
        }
        
        return prefix + "/level2check";
    }
    /**
     * level1
     * @return
     */
    @GetMapping("/level3")
    public String level3(String taskid, ModelMap mmap){
        Task t = taskService.createTaskQuery().taskId(taskid).singleResult();
        String processId = t.getProcessInstanceId();
        ProcessInstance p = runtimeService.createProcessInstanceQuery().processInstanceId(processId).singleResult();
        if (p != null) {
        	GcBszbzfd gcBszbzfd = gcBszbzfdService.selectGcBszbzfdById(Long.parseLong(p.getBusinessKey().toString()));
        	GcBszbzfditem gcBszbzfditem = new GcBszbzfditem();
        	gcBszbzfditem.setBszbzfdId(gcBszbzfd.getId());
            List<GcBszbzfditem> items = gcBszbzfditemService.selectGcBszbzfditemList(gcBszbzfditem);
            gcBszbzfd.setGcBszbzfditem(items);
            
            if(gcBszbzfd.getZilwj()!=null && !gcBszbzfd.getZilwj().equals("")){
            	String[] zlis = gcBszbzfd.getZilwj().split(",");
            	String file = "";
            	for(int i=0;i<zlis.length;i++){
            		file += zlis[i]+",";
            	}
            	gcBszbzfd.setZilwj(file);
            }
            mmap.put("obj", gcBszbzfd);
        	
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            mmap.put("taskid", taskid);
            
            //下一级审核人
            mmap.put("approveid", gcBszbzfd.getFzjl1Id());
            mmap.put("approvename", gcBszbzfd.getFzjl1());
            
          //所属
            SysDept dept = new SysDept();
            dept.setDelFlag("1");
            dept.setParentId(100l);
            List<SysDept> deptlis = deptService.selectDeptList4pId(dept);
            mmap.put("deptlis", deptlis);
        }
        
        return prefix + "/level3check";
    }
    /**
     * level1
     * @return
     */
    @GetMapping("/level4")
    public String level4(String taskid, ModelMap mmap){
        Task t = taskService.createTaskQuery().taskId(taskid).singleResult();
        String processId = t.getProcessInstanceId();
        ProcessInstance p = runtimeService.createProcessInstanceQuery().processInstanceId(processId).singleResult();
        if (p != null) {
        	GcBszbzfd gcBszbzfd = gcBszbzfdService.selectGcBszbzfdById(Long.parseLong(p.getBusinessKey().toString()));
        	GcBszbzfditem gcBszbzfditem = new GcBszbzfditem();
        	gcBszbzfditem.setBszbzfdId(gcBszbzfd.getId());
            List<GcBszbzfditem> items = gcBszbzfditemService.selectGcBszbzfditemList(gcBszbzfditem);
            gcBszbzfd.setGcBszbzfditem(items);
            
            if(gcBszbzfd.getZilwj()!=null && !gcBszbzfd.getZilwj().equals("")){
            	String[] zlis = gcBszbzfd.getZilwj().split(",");
            	String file = "";
            	for(int i=0;i<zlis.length;i++){
            		file += zlis[i]+",";
            	}
            	gcBszbzfd.setZilwj(file);
            }
            mmap.put("obj", gcBszbzfd);
        	
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            mmap.put("taskid", taskid);
            
            //下一级审核人
            mmap.put("approveid", gcBszbzfd.getFzjl2Id());
            mmap.put("approvename", gcBszbzfd.getFzjl2());
            
          //所属
            SysDept dept = new SysDept();
            dept.setDelFlag("1");
            dept.setParentId(100l);
            List<SysDept> deptlis = deptService.selectDeptList4pId(dept);
            mmap.put("deptlis", deptlis);
        }
        
        return prefix + "/level4check";
    }
    /**
     * level1
     * @return
     */
    @GetMapping("/level5")
    public String level5(String taskid, ModelMap mmap){
        Task t = taskService.createTaskQuery().taskId(taskid).singleResult();
        String processId = t.getProcessInstanceId();
        ProcessInstance p = runtimeService.createProcessInstanceQuery().processInstanceId(processId).singleResult();
        if (p != null) {
        	GcBszbzfd gcBszbzfd = gcBszbzfdService.selectGcBszbzfdById(Long.parseLong(p.getBusinessKey().toString()));
        	GcBszbzfditem gcBszbzfditem = new GcBszbzfditem();
        	gcBszbzfditem.setBszbzfdId(gcBszbzfd.getId());
            List<GcBszbzfditem> items = gcBszbzfditemService.selectGcBszbzfditemList(gcBszbzfditem);
            gcBszbzfd.setGcBszbzfditem(items);
            
            if(gcBszbzfd.getZilwj()!=null && !gcBszbzfd.getZilwj().equals("")){
            	String[] zlis = gcBszbzfd.getZilwj().split(",");
            	String file = "";
            	for(int i=0;i<zlis.length;i++){
            		file += zlis[i]+",";
            	}
            	gcBszbzfd.setZilwj(file);
            }
            mmap.put("obj", gcBszbzfd);
        	
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            mmap.put("taskid", taskid);
            
            //下一级审核人
            mmap.put("approveid", gcBszbzfd.getZjlId());
            mmap.put("approvename", gcBszbzfd.getZjl());
            
          //所属
            SysDept dept = new SysDept();
            dept.setDelFlag("1");
            dept.setParentId(100l);
            List<SysDept> deptlis = deptService.selectDeptList4pId(dept);
            mmap.put("deptlis", deptlis);
        }
        
        return prefix + "/level5check";
    }
    
    /**
     * level1
     * @return
     */
    @GetMapping("/level6")
    public String level6(String taskid, ModelMap mmap){
        Task t = taskService.createTaskQuery().taskId(taskid).singleResult();
        String processId = t.getProcessInstanceId();
        ProcessInstance p = runtimeService.createProcessInstanceQuery().processInstanceId(processId).singleResult();
        if (p != null) {
        	GcBszbzfd gcBszbzfd = gcBszbzfdService.selectGcBszbzfdById(Long.parseLong(p.getBusinessKey().toString()));
        	GcBszbzfditem gcBszbzfditem = new GcBszbzfditem();
        	gcBszbzfditem.setBszbzfdId(gcBszbzfd.getId());
            List<GcBszbzfditem> items = gcBszbzfditemService.selectGcBszbzfditemList(gcBszbzfditem);
            gcBszbzfd.setGcBszbzfditem(items);
            
            if(gcBszbzfd.getZilwj()!=null && !gcBszbzfd.getZilwj().equals("")){
            	String[] zlis = gcBszbzfd.getZilwj().split(",");
            	String file = "";
            	for(int i=0;i<zlis.length;i++){
            		file += zlis[i]+",";
            	}
            	gcBszbzfd.setZilwj(file);
            }
            mmap.put("obj", gcBszbzfd);
        	
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            mmap.put("taskid", taskid);
            
            //下一级审核人
            mmap.put("approveid", gcBszbzfd.getCnId());
            mmap.put("approvename", gcBszbzfd.getCn());
            
          //所属
            SysDept dept = new SysDept();
            dept.setDelFlag("1");
            dept.setParentId(100l);
            List<SysDept> deptlis = deptService.selectDeptList4pId(dept);
            mmap.put("deptlis", deptlis);
        }
        
        return prefix + "/level6check";
    }
    
    /**
     * level1
     * @return
     */
    @GetMapping("/level7")
    public String level7(String taskid, ModelMap mmap){
        Task t = taskService.createTaskQuery().taskId(taskid).singleResult();
        String processId = t.getProcessInstanceId();
        ProcessInstance p = runtimeService.createProcessInstanceQuery().processInstanceId(processId).singleResult();
        if (p != null) {
        	GcBszbzfd gcBszbzfd = gcBszbzfdService.selectGcBszbzfdById(Long.parseLong(p.getBusinessKey().toString()));
        	GcBszbzfditem gcBszbzfditem = new GcBszbzfditem();
        	gcBszbzfditem.setBszbzfdId(gcBszbzfd.getId());
            List<GcBszbzfditem> items = gcBszbzfditemService.selectGcBszbzfditemList(gcBszbzfditem);
            gcBszbzfd.setGcBszbzfditem(items);
            
            if(gcBszbzfd.getZilwj()!=null && !gcBszbzfd.getZilwj().equals("")){
            	String[] zlis = gcBszbzfd.getZilwj().split(",");
            	String file = "";
            	for(int i=0;i<zlis.length;i++){
            		file += zlis[i]+",";
            	}
            	gcBszbzfd.setZilwj(file);
            }
            mmap.put("obj", gcBszbzfd);
        	
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            mmap.put("taskid", taskid);
            
            //下一级审核人
            List<SjProcessConfig> configlis = returnUser(Long.valueOf(gcBszbzfd.getDeptId().toString()));
            mmap.put("approveid", configlis.get(0).getLevel8id());
            mmap.put("approvename", configlis.get(0).getLevel8name());
            
          //所属
            SysDept dept = new SysDept();
            dept.setDelFlag("1");
            dept.setParentId(100l);
            List<SysDept> deptlis = deptService.selectDeptList4pId(dept);
            mmap.put("deptlis", deptlis);
        }
        
        return prefix + "/level7check";
    }
    
    /**
     * 修改单据
     * @param taskid
     * @param mmap
     * @return
     */
    @GetMapping("/modify")
    public String modify(String taskid, ModelMap mmap){
        Task t = taskService.createTaskQuery().taskId(taskid).singleResult();
        String processId = t.getProcessInstanceId();
        ProcessInstance p = runtimeService.createProcessInstanceQuery().processInstanceId(processId).singleResult();
        if (p != null) {
        	GcBszbzfd gcBszbzfd = gcBszbzfdService.selectGcBszbzfdById(Long.parseLong(p.getBusinessKey().toString()));
        	GcBszbzfditem gcBszbzfditem = new GcBszbzfditem();
        	gcBszbzfditem.setBszbzfdId(gcBszbzfd.getId());
            List<GcBszbzfditem> items = gcBszbzfditemService.selectGcBszbzfditemList(gcBszbzfditem);
            gcBszbzfd.setGcBszbzfditem(items);
            
            if(gcBszbzfd.getZilwj()!=null && !gcBszbzfd.getZilwj().equals("")){
            	String[] zlis = gcBszbzfd.getZilwj().split(",");
            	String file = "";
            	for(int i=0;i<zlis.length;i++){
            		file += zlis[i]+",";
            	}
            	gcBszbzfd.setZilwj(file);
            }
            mmap.put("obj", gcBszbzfd);
        	
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            mmap.put("taskid", taskid);
            
            GcSupply gcSupply = new GcSupply();
        	gcSupply.setDeleteFlag(1);
//        	gcSupply.setType(6);
        	List<GcSupply> gcSupplylis = gcSupplyService.selectGcSupplyList(gcSupply);
        	mmap.put("clis", gcSupplylis);
        	
        	//所属
            SysDept dept = new SysDept();
            dept.setDelFlag("1");
            dept.setParentId(100l);
            List<SysDept> deptlis = deptService.selectDeptList4pId(dept);
            mmap.put("deptlis", deptlis);
        }
        
        return prefix + "/modifycheck";
    }
}